package com.mubolg.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.json.JSONUtil;
import com.mubolg.properties.SaTokenSettingProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Slf4j
@EnableConfigurationProperties({SaTokenSettingProperties.class})
public class StpInterfaceImpl implements StpInterface {

    private  RedisTemplate redisTemplate;
    private  SaTokenSettingProperties saTokenSettingProperties;
    public StpInterfaceImpl (RedisTemplate redisTemplate,SaTokenSettingProperties saTokenSettingProperties){
       this.redisTemplate = redisTemplate;
       this.saTokenSettingProperties = saTokenSettingProperties;
    }


    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //TODO 要去redis 获取用户的权限
        String key = String.format(saTokenSettingProperties.getKey(), loginId);
        Object permissionsObj = redisTemplate.opsForValue().get(key);
        List<String> list = JSONUtil.toList(JSONUtil.parseArray(permissionsObj), String.class);
        log.info("权限列表：{}",list);
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return new ArrayList<String>();
    }

}